package cn.disicn.controller;

import java.util.Date;
import org.apache.commons.lang3.StringUtils;
import cn.disicn.common.Const;
import cn.disicn.common.ServerResponse;
import cn.disicn.model.AppUser;
import cn.disicn.model.BizJobs;
import cn.disicn.model.BizJobsOrder;
import cn.disicn.model.BizJobsOrderSubmitResult;
import cn.disicn.model.BizJobsType;
import com.jfinal.core.Controller;

/**
 * @author zjk
 * @date 2018年1月13日 下午3:23:59
 * @desc
 */
public class BizJobsOrderSubmitResultController extends Controller {

    private BizJobsOrderSubmitResult bizJobsorderSubmitresult = new BizJobsOrderSubmitResult();
    private BizJobsOrder bizJobsOrder = new BizJobsOrder();
    private BizJobs bizJobs = new BizJobs();
    private AppUser appUser = new AppUser();
    private BizJobsType bizJobsType = new BizJobsType();

    /**
     * @return
     * @desc审核修改状态
     */
    public void updateBizJobsOrderSubmitResultStatus() {
        String id = getPara("id");
        String type = getPara("type");
        String price = getPara("price");
        if (bizJobsorderSubmitresult.bizJobsOrderSubmitResultById(id).isSuccess()) {
            if (!StringUtils.isBlank(type)) {
                if (type.equals(Const.BizJobsOrderConfirmResult.NOPASS)) {
                    bizJobsOrder.setConfirmResult(Const.BizJobsOrderConfirmResult.NOPASS);
                    bizJobsorderSubmitresult.setAuditResult(Const.BizJobsOrderSubmitResultAuditResult.NOPASS);
                    bizJobsOrder.setCancelTime(new Date());
                } else if (type.equals(Const.BizJobsOrderConfirmResult.PASS)) {
                    bizJobsOrder.setConfirmResult(Const.BizJobsOrderConfirmResult.PASS);
                    bizJobsorderSubmitresult.setAuditResult(Const.BizJobsOrderSubmitResultAuditResult.PASS);
                    bizJobsOrder.setConfirmTime(new Date());
                    appUser.addAccountAmount(id, price);
                }
                bizJobsorderSubmitresult.setId(id);
                bizJobsorderSubmitresult.setAuditTime(new Date());
                bizJobsOrder.setStatus(Const.BizJobsOrderStatus.OVERDUE);
                ServerResponse<String> response = bizJobsorderSubmitresult
                        .updateBizJobsOrderSubmitResultStatus(bizJobsorderSubmitresult);
                ServerResponse<String> response1 = bizJobsOrder.updateBizJobsOrder(bizJobsOrder, id, type);
                if (bizJobsType.getBizJobsTypeNameByResultId(id).isSuccess()) {
                    ServerResponse<String> response2 = bizJobs.updateBizJobsStatus(Const.TaskStatusEnum.OVER.getCode() + "", id);
                }
                if (response.isSuccess() && response1.isSuccess()) {
                    renderJson(ServerResponse.createBySuccess());
                    return;
                }
            }
        }
        renderJson(ServerResponse.createByError());
    }
}
